package com.app.studyroom.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.app.studyroom.R;
import com.app.studyroom.api.model.ssrBooking.BookingItem;
import com.app.studyroom.mapper.ApprovalStatusMapper;
import com.app.studyroom.mapper.SeatMapper;

import java.util.List;

public class BookingAdapter extends RecyclerView.Adapter<BookingAdapter.BookingViewHolder> {

    private List<BookingItem> bookingList;
    private Context context;
    private OnCancelClickListener onCancelClickListener;

    public interface OnCancelClickListener {
        void onCancelClick(BookingItem item);
    }

    public BookingAdapter(Context context, List<BookingItem> bookingList) {
        this.context = context;
        this.bookingList = bookingList;
    }

    public void setOnCancelClickListener(OnCancelClickListener listener) {
        this.onCancelClickListener = listener;
    }


    //开始学习结束学习监听
    public interface OnStartStudyClickListener {
        void onStartStudyClick(BookingItem item);
    }
    public interface OnEndStudyClickListener {
        void onEndStudyClick(BookingItem item);
    }
    private OnStartStudyClickListener onStartStudyClickListener;
    private OnEndStudyClickListener onEndStudyClickListener;
    public void setOnStartStudyClickListener(OnStartStudyClickListener listener) {
        this.onStartStudyClickListener = listener;
    }
    public void setOnEndStudyClickListener(OnEndStudyClickListener listener) {
        this.onEndStudyClickListener = listener;
    }



    @NonNull
    @Override
    public BookingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_booking, parent, false);
        return new BookingViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BookingViewHolder holder, int position) {
        BookingItem item = bookingList.get(position);

        //显示真实的房间和座位号
        String RoomPlace = item.getRoomPlace();
        String SeatNum = item.getSeatNum();
        String RoomPlaceDisplay=SeatMapper.getFrontendRoom(RoomPlace);
        Integer SeatNumDisplay  =SeatMapper.getFrontendSeatId(SeatNum);
        holder.tvInfo.setText(RoomPlaceDisplay +"-" + SeatNumDisplay);

        //开始和结束的真正日期
        String startTime = item.getBookingStartTime(); // 2025-05-14 14:59:00
        String endTime = item.getBookingEndTime();     // 2025-05-14 15:59:00

        // 显示只保留月日时，去除年部分
        String startDisplay = startTime.length() >= 16 ? startTime.substring(5, 16) : startTime;
        String endDisplay = endTime.length() >= 16 ? endTime.substring(5, 16) : endTime;
        holder.tvTime.setText(startDisplay + " - " + endDisplay);

        //获取座位预约状态
        String approvalStatus = item.getApprovalStatus();
        String approvalStatusDisplay = ApprovalStatusMapper.getNameByCode(approvalStatus);
        holder.tvApprovalStatus.setText(approvalStatusDisplay);

        //开始学习结束学习
        holder.btnStartStudy.setOnClickListener(v -> {
            if (onStartStudyClickListener != null) {
                onStartStudyClickListener.onStartStudyClick(item);
            }
        });
        holder.btnEndStudy.setOnClickListener(v -> {
            if (onEndStudyClickListener != null) {
                onEndStudyClickListener.onEndStudyClick(item);
            }
        });
        holder.btnCancel.setOnClickListener(v -> {
            if (onCancelClickListener != null) {
                onCancelClickListener.onCancelClick(item);
            }
        });

        // ✅ 根据状态控制按钮显示
        holder.btnCancel.setVisibility(View.GONE);
        holder.btnStartStudy.setVisibility(View.GONE);
        holder.btnEndStudy.setVisibility(View.GONE);

        switch (approvalStatus) {

            case "0": // 无审批
                holder.btnCancel.setVisibility(View.VISIBLE);
                holder.btnStartStudy.setVisibility(View.VISIBLE);
                break;
            case "1": // 待审批
//                holder.btnCancel.setVisibility(View.VISIBLE);
                break;
            case "2": // 已审批,缺开始学习
                holder.btnCancel.setVisibility(View.VISIBLE);
                holder.btnStartStudy.setVisibility(View.VISIBLE);
                break;
            case "3": // 审批驳回
                break;
            case "4": // 学习中
                holder.btnEndStudy.setVisibility(View.VISIBLE);
                break;
            case "5": // 学习结束
                break;
            case "6": // 超时违规
                break;

        }


    }
    @Override
    public int getItemCount() {
        return bookingList.size();
    }
    static class BookingViewHolder extends RecyclerView.ViewHolder {
        TextView tvInfo, tvTime, tvApprovalStatus;
        Button btnCancel;
        Button btnStartStudy, btnEndStudy;

        public BookingViewHolder(@NonNull View itemView) {
            super(itemView);
            tvInfo = itemView.findViewById(R.id.tv_booking_info);
            tvTime = itemView.findViewById(R.id.tv_booking_time);
            tvApprovalStatus = itemView.findViewById(R.id.tv_approval_status);
            btnCancel = itemView.findViewById(R.id.btn_cancel_booking);
            btnStartStudy = itemView.findViewById(R.id.btn_start_study);
            btnEndStudy = itemView.findViewById(R.id.btn_end_study);
        }
    }
}

